package com.sjyl.kaoshitong.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.sjyl.kaoshitong.R;

import java.util.List;

/**
 * Created by ChangerLow on 15/4/25.
 * Email: changerlow@qq.com
 */
public class MyDocumentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private static final String TAG = "MyDocumentRecyclerViewAdapter";
    private List<String> dataSet = null;
    private MyDocumentFileViewHolder.MyOnItemClickListener mItemClickListener;
    public MyDocumentRecyclerViewAdapter(List<String> list){
        this.dataSet = list;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_document_folder_layout, parent, false);
        return new MyDocumentFileViewHolder(view, mItemClickListener);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        MyDocumentFileViewHolder holder = (MyDocumentFileViewHolder)viewHolder;
        holder.titleTV.setText(dataSet.get(position));
        holder.subtitleTV.setText("this is subtitle");
        holder.idTV.setText("id");
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public void setOnItemClickListener(MyDocumentFileViewHolder.MyOnItemClickListener listener){
        this.mItemClickListener = listener;
    }

    public static class MyDocumentFileViewHolder extends RecyclerView.ViewHolder {
        public TextView titleTV;
        public TextView subtitleTV;
        public TextView idTV;

        public MyDocumentFileViewHolder(final View itemView, final MyOnItemClickListener listener) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null)
                        listener.OnItemClick(v, getAdapterPosition());
                    //AlertDialog dialog =new AlertDialog.Builder(itemView.getContext()).setTitle("This is a Dialog").setMessage("You click "+ getAdapterPosition()+" element!").show();
                    Logger.d(TAG, "Element "+ getAdapterPosition() + " clicked.");
                }
            });
            titleTV = (TextView)itemView.findViewById(R.id.list_item_title);
            subtitleTV = (TextView)itemView.findViewById(R.id.list_item_subtitle);
            idTV = (TextView)itemView.findViewById(R.id.list_item_id_label);
        }

        public interface MyOnItemClickListener{
            void OnItemClick(View view, int position);
        }
    }
}
